package org.adream.account.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.security.cas.authentication.CasAuthenticationToken;

public class UserUtil {

	/**
	 * 返回用户的UID 若用户没有登录，则返回NULL
	 * @param request
	 * @return
	 */
	public static String getUidByRequest(HttpServletRequest request){
		Map<String,Object> map;
		Object o = new Object();
		o = request.getUserPrincipal();
		if(o == null ||!(o instanceof CasAuthenticationToken)){
			return null;
		}
		CasAuthenticationToken p = (CasAuthenticationToken)o;
		Assertion assertion = p.getAssertion();
		AttributePrincipal principal = assertion.getPrincipal();
		map = principal.getAttributes();
		return (String) map.get("uid");
	}
}
